package com.amon.amonplayer.monitor;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Looper;
import android.os.MessageQueue;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class MonitorImageView extends ImageView implements MessageQueue.IdleHandler {
    private static final int MAX_ALARM_IMAGE_SIZE = 33 * 1024 * 1024;
    private static final int MAX_ALARM_MULTIPLE = 2;

    public MonitorImageView(@NonNull Context context) {
        super(context);
    }

    public MonitorImageView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean queueIdle() {
        try {
            Drawable drawable = getDrawable();
            Drawable background = getBackground();
            if (drawable != null) {
                checkIsLegal(drawable, "图片");
            }
            if (background != null) {
                checkIsLegal(drawable, "背景");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    private void checkIsLegal(Drawable drawable, String tag) {
        int viewWidth = getMeasuredWidth();
        int viewHeight = getMeasuredHeight();
        int drawableWidth = drawable.getIntrinsicWidth();
        int drawableHeight = drawable.getIntrinsicHeight();

        int imageSize = calculateImageSize(drawable);
        if (imageSize > MAX_ALARM_IMAGE_SIZE) {
            dealWarning(drawableWidth, drawableHeight, imageSize, drawable);
        }
        // 宽高告警判断
        if (MAX_ALARM_MULTIPLE * viewWidth < drawableWidth) {
            Log.e("TAG", "图片加载不合法, 控件宽度 -> " + viewWidth + " , " + tag + "宽度 -> " + drawableWidth);
            dealWarning(drawableWidth, drawableHeight, imageSize, drawable);
        }
        if (MAX_ALARM_MULTIPLE * viewHeight < drawableHeight) {
            Log.e("TAG", "图片加载不合法, 控件高度 -> " + viewHeight + " , " + tag + "高度 -> " + drawableHeight);
            dealWarning(drawableWidth, drawableHeight, imageSize, drawable);
        }
    }

    private void dealWarning(int drawableWidth, int drawableHeight, int imageSize, Drawable drawable) {

    }

    private int calculateImageSize(Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
            return bitmap.getByteCount();
        }
        int pixelSize = drawable.getOpacity() != PixelFormat.OPAQUE ? 4 : 2;
        return pixelSize * drawable.getIntrinsicHeight() * drawable.getIntrinsicWidth();
    }

    @Override
    public void setBackgroundDrawable(Drawable background) {
        super.setBackgroundDrawable(background);
        addImageLegalMonitor();
    }

    @Override
    public void setImageDrawable(@Nullable Drawable drawable) {
        super.setImageDrawable(drawable);
        addImageLegalMonitor();
    }

    private void addImageLegalMonitor() {
        Looper.myQueue().removeIdleHandler(this);
        Looper.myQueue().addIdleHandler(this);
    }
}
